import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;

public class homePage extends MainFrame
{
  int countAll, countToday;
  static String dateToday;
  
  JLabel title;
  JTable table;
  JScrollPane scrollPane;
  String row[][] = {{"Date","Time","Patient","Service Type"}};
  String col[] = {"Date","Time","Patient","Service Type"};
  JButton btnAllAppointments;
  static DateAndTime getDate = new DateAndTime();
  String Date, Time;
  AlphaImageIcon hi;
  
  ArrayList <Integer> key = new ArrayList();
  
   public void Draw(){
       /* Labels */
       title = new JLabel("Welcome!");
       title.setFont(new Font("Cambria", Font.PLAIN, 24));
       title.setForeground(Color.black);
       title.setBounds(10,215,300,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("_____________________________________________________________");
       title.setFont(new Font("Cambria", Font.BOLD, 14));
       title.setForeground(Color.black);
       title.setBounds(0,220,400,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("Upcoming Appointments");
       title.setFont(new Font("Cambria", Font.PLAIN, 24));
       title.setForeground(Color.black);
       title.setBounds(10,345,300,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("_____________________________________________________________");
       title.setFont(new Font("Cambria", Font.BOLD, 14));
       title.setForeground(Color.black);
       title.setBounds(0,350,400,50);
       MainFrame.panel.add(title);
       
       
        // show current date and time
        getDate.GetDateAndTime();
        
        // For Date
        String formatteddate1 = String.format("%02d", DateAndTime.day);
        String formatteddate2 = String.format("%02d", DateAndTime.month+1);
        String formatteddate3 = String.format("%02d", DateAndTime.year);
            
        // For Time
        String formatted1 = String.format("%02d", DateAndTime.hour);
        String formatted2 = String.format("%02d", DateAndTime.minute);
        String formatted3 = String.format("%02d", DateAndTime.second);
        
        if(DateAndTime.am_pm.equals("PM"))
        Time = (DateAndTime.hour+12) + ":" + formatted2 + ":" + formatted3;
        else
        Time = DateAndTime.hour + ":" + formatted2 + ":" + formatted3;

       /* View All Appointments Button */
       btnAllAppointments = new JButton(new ImageIcon("allAppointmentsButton.jpg"));
       btnAllAppointments.setBounds(230,530,215,25);
       btnAllAppointments.addActionListener(new ButtonListener());
       MainFrame.panel.add(btnAllAppointments);
       

       
            /* Appointments Table */
            DefaultTableModel model = new DefaultTableModel(row,col) {
                /* Override Cell modification for users not to modify data inside table */
                @Override
                public boolean isCellEditable(int row, int column) {
                    //all cells false
                    return false;
                }
            };;
            model.getDataVector().removeAllElements();
            table = new JTable(model);
            key.clear();
         try{
                        database.stmt = database.conn.createStatement();
                        
                               
                        database.rs = database.stmt.executeQuery( 
                                "SELECT appointmentID, DATE_FORMAT(NOW(), '%M %d %Y') as appointmentDate FROM APPOINTMENT");
                                
                                while( database.rs.next() ){
                                    dateToday = database.rs.getString("appointmentDate");
                           }
                                
                        database.rs = database.stmt.executeQuery( 
                                "SELECT appointmentID, DATE_FORMAT(appointmentDate, '%M %d %Y') as appointmentDate,startTime, endTime, lastName,firstName, serviceName FROM APPOINTMENT p,PATIENT q, SERVICE r WHERE p.SERVICEID = r.SERVICEID" 
                                + " AND p.PATIENTID = q.PATIENTID AND p.STATUS LIKE 'Pending' ORDER BY p.appointmentDate ASC, p.startTime ASC");
                                
                                while( database.rs.next() ){
                                    key.add(database.rs.getInt("appointmentID"));
                                    model.insertRow(table.getRowCount(),new Object[]{
                                        database.rs.getString("appointmentDate").equals(dateToday) ? "Today" : database.rs.getString("appointmentDate"),
                                        database.rs.getString("startTime") + "-" + database.rs.getString("endTime"),
                                        database.rs.getString("lastname")+", "+ database.rs.getString("firstName"),
                                        database.rs.getString("serviceName")
                                    });
                           }
             } catch(SQLException se){}
             
         try{
                        database.stmt = database.conn.createStatement();
                               
                        database.rs = database.stmt.executeQuery( "SELECT COUNT(*) AS countAppointments FROM APPOINTMENT WHERE STATUS LIKE 'Pending'");  
                                
                                while( database.rs.next()) {
                                    countAll = database.rs.getInt("countAppointments");}
                        
                        database.rs = database.stmt.executeQuery( "SELECT COUNT(*) AS countAppointments FROM APPOINTMENT WHERE STATUS LIKE 'Pending' AND appointmentDate LIKE '"+
                                formatteddate3+"-"+formatteddate2+"-"+formatteddate1+"'");
                               
                                while( database.rs.next() ){
                                    countToday = database.rs.getInt("countAppointments");}
                                
                              
                        title = new JLabel("<HTML>Today is " + dateToday + ", " + formatted1 + ":" + formatted2 + " " + DateAndTime.am_pm +
                          "<BR>You have ("+countToday+" of "+countAll+") total pending appointments scheduled today.</HTML>");
                           
                           
                        
             } catch(SQLException se){}
       
         title.setFont(new Font("Cambria", Font.PLAIN, 16));
         title.setForeground(Color.black);
         title.setBounds(30,265,600,70);
         MainFrame.panel.add(title);
            table.setFont(new java.awt.Font("Cambria", Font.PLAIN ,15));
            table.getTableHeader().setFont( new Font( "Cambria" , Font.PLAIN , 15 ));
            table.getTableHeader().setBackground( new Color(0, 120, 255, 70) );
            table.getTableHeader().setEnabled(false);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.addMouseListener(new TableListener());
            scrollPane = new JScrollPane(table);
            scrollPane.setBounds(30,400,680,117);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getViewport().setOpaque(false);
            MainFrame.panel.add(scrollPane);

  }
  
  
  
  
  public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){
       if(e.getSource() == btnAllAppointments){
          // Go to Appointment list
          MainFrame.panel.removeAll();
          MainFrame.page = 4;
          MainFrame.appointmentsPage.page = 1;
          MainFrame.appointmentsPage.Draw();
          MainFrame.header.Draw();
          MainFrame.panel.add(MainFrame.backgroundImage);
          MainFrame.panel.repaint();
       }
    }
    }
    
    
  /* Listener for Appointments Table */
   class TableListener extends MouseAdapter {
   public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() == 2 && e.getSource() == table) {
         JTable target = (JTable)e.getSource();
         
         // To know the exact location of the mouse click
         appointmentsPage.picked = key.get(target.getSelectedRow());
         
         // Go to Appointment details on mouse click
          MainFrame.panel.removeAll();
          MainFrame.page = 4;
          MainFrame.appointmentsPage.page = 2;
          MainFrame.appointmentsPage.Draw();
          MainFrame.header.Draw();
          MainFrame.panel.add(MainFrame.backgroundImage);
          MainFrame.panel.repaint();
         }
   }
   }

}
